package com.wxlsoft.excel.convert.listeners;

import javax.swing.JProgressBar;

import com.alibaba.excel.context.AnalysisContext;
import com.wxlsoft.excel.convert.handlers.ServicePackageConverter;
import com.wxlsoft.excel.convert.handlers.ServicePackageParser;
import com.wxlsoft.excel.convert.model.BasePackageRecord;
import com.wxlsoft.excel.convert.model.ServiceBody;
import com.wxlsoft.excel.convert.model.ServicePackage;
import com.wxlsoft.excel.convert.model.SheetDataConvertCollector;

import lombok.NonNull;

/**
 * 公共报文头
 * @author wangxiaoliang
 */
public class ServicePackageListener extends AbstractPackageListener {

	private final ServicePackage service;
	
	public ServicePackageListener(String sheetName, boolean isStandard, JProgressBar progress,
			@NonNull final SheetDataConvertCollector collector) {
		super(sheetName, progress, collector);
		this.service = new ServicePackage(sheetName, isStandard);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected boolean isSkip(BasePackageRecord data, AnalysisContext context) {
		Integer rowNum = context.getCurrentRowNum();
		return rowNum == 5 || rowNum == 6;
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void serviceHead(BasePackageRecord data, AnalysisContext context) {
		Integer rowNum = context.getCurrentRowNum();
		ServicePackageParser.toServiceHead(service.getHead(), data, rowNum);
	}

	@Override
	protected void serviceBodyInput(BasePackageRecord data, AnalysisContext context) {
		ServiceBody bodyInput = ServicePackageParser.toServiceBody(data);
		service.addServiceBodyInput(bodyInput);
	}

	@Override
	protected void serviceBodyOutput(BasePackageRecord data, AnalysisContext context) {
		ServiceBody bodyOutput = ServicePackageParser.toServiceBody(data);
		service.addServiceBodyOutput(bodyOutput);
	}

	@Override
	protected void doAfter(AnalysisContext context) {
		ServicePackageConverter.convertServicePackage(service);
		collector.addServicePackage(service);
	}

}
